package musicstore.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import musicstore.dao.OrderDao;
import musicstore.entity.Order;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class OrderDaoImpl extends HibernateDaoSupport implements OrderDao {

	@Override
	public void add(Order order) {
		getHibernateTemplate().save(order);
	}
	

	@Override
	public List<Order> getListByDeliver(int isDeliveried) {
		String hql = "from Order where isDeliveried = ?";
		List<Order> list = (List<Order>) getHibernateTemplate().find(hql,isDeliveried);
		return list;
	}

	@Override
	public List<Order> getListByDate(int isDeliveried,int orderId, Date startDate,
			Date endDate) {
		ArrayList list = new ArrayList();
		String hql = "from Order where 1=1 and isDeliveried = ? ";
		list.add(isDeliveried);
		if(startDate!=null){
			hql += "and orderDate > ? ";
			list.add( startDate);
		}
		if(orderId>0){
			hql+="and id = ? ";
			list.add(orderId);
		}
		if(endDate!=null){
			hql += "and orderDate < ?";
			Calendar cal = Calendar.getInstance();
			cal.setTime(endDate);
			cal.add(cal.DATE, +1);
			list.add(cal.getTime());
		}
		return (List<Order>) getHibernateTemplate().find(hql,list.toArray());
	}
	
	public static void main(String[] args) {

	}


	@Override
	public Order getOrderById(int id) {
		return getHibernateTemplate().get(Order.class, id);
	}

}
